package com.jungle.mvvm.sample.ui.chat;

import android.annotation.SuppressLint;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;


import com.gyf.immersionbar.ImmersionBar;
import com.gyf.immersionbar.components.SimpleImmersionOwner;
import com.gyf.immersionbar.components.SimpleImmersionProxy;
import com.jungle.mvvm.sample.R;
import com.jungle.mvvm.sample.app.WanObserver;
import com.jungle.mvvm.sample.bean.KindBean;
import com.jungle.mvvm.sample.bean.SameBean;
import com.jungle.mvvm.sample.http.WanNetwork;
import com.jungle.mvvm.sample.ui.base.fragment.BasePagerFragment;
import com.jungle.mvvm.utils.KLog;

import java.util.ArrayList;
import java.util.List;

/**
 * 详细说明
 *
 * @author Daniel
 * @version V1.0
 * @date 2020/3/5 17:58
 */
public class ChatFragment extends BasePagerFragment implements SimpleImmersionOwner {
    private List<KindBean> mKindBeans = new ArrayList<>();
    List<Fragment> mFragments = new ArrayList<>();
    List<String> mTitles = new ArrayList<>();
    List<Boolean> mVisibles = new ArrayList<>();

    @SuppressLint("CheckResult")
    private void getChapterTree() {
        ChatModel
                .getChapterTree()
                .compose(WanNetwork.applyService(this, new WanObserver<SameBean<List<KindBean>>>() {
                    @Override
                    public void onWanSuccess(SameBean<List<KindBean>> listWanBean) {
                        mKindBeans = listWanBean.getData();
                        ChatKindFragment fragment;
                        for (KindBean kindBean : mKindBeans) {
                            fragment = ChatKindFragment.newInstance(kindBean.getId());
                            mFragments.add(fragment);
                            mVisibles.add(false);
                            mTitles.add(kindBean.getName());
                        }
                    }

                    @Override
                    public void onWanFailure(String e) {
                        KLog.e(e);
                    }
                }));
    }

    @Override
    protected List<Fragment> pagerFragment() {
        mVisibles.set(0, true);
        binding.viewPager.setOffscreenPageLimit(mFragments.size());
        return mFragments;
    }

    @Override
    protected List<String> pagerTitleString() {
        return mTitles;
    }

    @Override
    protected void tabSelected(int position) {
        Boolean visible = mVisibles.get(position);
        if (!visible) {
            mVisibles.set(position, true);
            ChatKindFragment fragment = (ChatKindFragment) mFragments.get(position);
            fragment.initFragment(mKindBeans.get(position).getId());
        }
    }

    /**
     * 创建Fragment
     *
     * @return ChatFragment
     */
    public static ChatFragment newInstance() {
        ChatFragment fragment;
        synchronized (ChatFragment.class) {
            fragment = new ChatFragment();
            fragment.getChapterTree();
        }
        return fragment;
    }

    /**
     * ImmersionBar代理类
     */
    private SimpleImmersionProxy mSimpleImmersionProxy = new SimpleImmersionProxy(this);

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        mSimpleImmersionProxy.setUserVisibleHint(isVisibleToUser);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mSimpleImmersionProxy.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        mSimpleImmersionProxy.onHiddenChanged(hidden);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mSimpleImmersionProxy.onConfigurationChanged(newConfig);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mSimpleImmersionProxy.onDestroy();
    }

    @Override
    public void initImmersionBar() {
        ImmersionBar.with(this)
                .fitsSystemWindows(true)
                .statusBarColor(R.color.white)
                .statusBarDarkFont(true)
                .init();
    }

    @Override
    public boolean immersionBarEnabled() {
        return true;
    }
}